package com.neu.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.neu.question.beans.HttpResponseEntity;
import com.neu.question.beans.ResultVO;
import com.neu.question.common.Constans;
import com.neu.question.domain.RespondentInfo;
import com.neu.question.domain.TenantsPropertyInfo;
import com.neu.question.domain.UserInfo;
import com.neu.question.form.EndUserForm;
import com.neu.question.form.TenantsForm;
import com.neu.question.service.TenantsPropertyInfoService;
import com.neu.question.service.UserInfoService;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class TenantsController {

  private final Logger logger = LoggerFactory.getLogger(TenantsController.class);
  @Autowired
  private TenantsPropertyInfoService tenantsPropertyInfoService;

  @Autowired
  private UserInfoService userInfoService;

  //查询所有的租户信息
  @GetMapping("/selectAllTenantsPropertyInfo")
  public List<Map<String, Object>> selectAllTenantsPropertyInfo() {
    return tenantsPropertyInfoService.selectAllTenantsPropertyInfo();
  }

  /**
   * 查询租户资产列表
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/queryTenantsListById", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity queryUserList(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      Map<String, Object> tenantsPropertyInfo = tenantsPropertyInfoService.selectTenantsPropertyInfoByUserId(map.get(
              "userId").toString());
      if (tenantsPropertyInfo != null) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(tenantsPropertyInfo);
        httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.QUERYFAIL_MESSAGE);
      }
    } catch (Exception e) {
      logger.error("查询资产信息异常>>>>>>>>>>>", e);
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 添加租户资产信息
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/addTenantsPropertyInfo", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity addTenantsPropertyInfo(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = tenantsPropertyInfoService.addTenantsPropertyInfo(map);
      if (result > 0) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
      }
    } catch (Exception e) {
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  @GetMapping("/tenantsSelfInfo/{name}")
  public ResultVO findById(@PathVariable("name") String id) throws ParseException {
    ResultVO resultVO = new ResultVO();
    QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("username", id);
    UserInfo one = userInfoService.getOne(queryWrapper);
//    System.out.println(one.getId());
    QueryWrapper<TenantsPropertyInfo> queryWrapper1 = new QueryWrapper<>();
    queryWrapper1.eq("user_id", one.getId());
    TenantsPropertyInfo byId = tenantsPropertyInfoService.getOne(queryWrapper1);
    TenantsForm te = new TenantsForm();
    te = te.create(one, byId.getProperty(), byId.getProjectNum());


//    RespondentInfo endUserInfo = respondentInfoService.getById(id);
//    UserInfo userInfo = userInfoService.getById(endUserInfo.getUserId());
//    EndUserForm endUserForm = new EndUserForm();
//    endUserForm = endUserForm.create(userInfo, endUserInfo.getEmail(), endUserInfo.getQuestionnaireId());
    resultVO.setCode(0);
    resultVO.setData(te);
    return resultVO;
  }

  @PutMapping("/tenantsSelfInfoUpdate")
  public ResultVO update(@RequestBody TenantsForm endUserForm) {
    //更新数据
    // 判断时间先后
    ResultVO resultVO = new ResultVO();
    int update;
//    System.out.println(12);
    update = tenantsPropertyInfoService.update(endUserForm);

    //  3重名 4重号码  5失败更新user
    resultVO.setCode(update);
    resultVO.setData(endUserForm);
    return resultVO;
  }


}
